package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.weixing.mall.base.constant.DatePattern;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.provider.mapper.OrderAgentMapper;
import com.weixing.mall.provider.model.domain.OrderAgent;
import com.weixing.mall.provider.model.query.AgentOrderQuery;
import com.weixing.mall.provider.model.vo.OrderAgentVo;
import com.weixing.mall.provider.model.vo.agent.UpPayReceiveVo;
import com.weixing.mall.provider.model.vo.agent.UpPayStatisticVo;
import com.weixing.mall.provider.service.IOrderAgentService;
import com.weixing.mall.util.number.NumberPatternEnum;
import com.weixing.mall.util.number.NumberToRmbUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 订单和 服务实现类
 * </p>
 *
 * @author Small
 * @since 2020-05-27
 */
@Service
public class OrderAgentServiceImpl extends ServiceImpl<OrderAgentMapper, OrderAgent> implements IOrderAgentService {

    @Override
    public UpPayReceiveVo countReceiveAndUpPay(Long agentId, String startTime, String endTime,Long selectedAgentId) {
        return baseMapper.countReceiveAndUpPay(agentId,startTime,endTime,selectedAgentId);
    }

    @Override
    public void updateEnable(Long orderId) {
        baseMapper.updateEnable(orderId);
    }

    @Override
    public IPage<OrderAgentVo> selectListOfPage(IPage page, Long agentId) {
        IPage<OrderAgentVo> agentVoIPage = baseMapper.selectListOfPage(page, agentId);
        return agentVoIPage;
    }

    /**
     * 需上交金额统计
     * @param query 查询条件
     * @return 需上交金额统计
     */
    @Override
    public UpPayStatisticVo upPayStatistics(AgentOrderQuery query) {
        // 需上交款
        UpPayReceiveVo cntResult = countReceiveAndUpPay(query.getAgentId(),query.getStartTime(),query.getEndTime(),query.getSelectedAgentId());
        BigDecimal upPayment = cntResult.getOwnUpPay();
        // 已上交款
        BigDecimal hasUpPay = baseMapper.sumNeedUppayBySettleState(query.getAgentId(),GlobalConstant.PROFIT_SETTLE_STATE_HASUP,query.getStartTime(),query.getEndTime(),query.getSelectedAgentId());
        UpPayStatisticVo vo = new UpPayStatisticVo();
        vo.setNeedUppay(NumberToRmbUtil.format(upPayment, NumberPatternEnum.PAT_DEFAULT.getValue()));
        vo.setHasUpPay(NumberToRmbUtil.format(hasUpPay, NumberPatternEnum.PAT_DEFAULT.getValue()));
        return vo;
    }

    @Override
    public BigDecimal todayProfit(Long agentId) {
        String date = DateUtil.format(new Date(),DatePattern.NORM_DATE_PATTERN);
        return baseMapper.todayProfit(agentId,date);
    }

    @Override
    public BigDecimal totalProfit(Long agentId, String startTime, String endTime) {
        return baseMapper.totalProfit(agentId,startTime,endTime);
    }

    @Override
    public OrderAgent findByOrderIdAndAgentId(Long orderId, Long agentId) {
        return baseMapper.findByOrderIdAndAgentId(orderId,agentId);
    }

    @Override
    public OrderAgent findKefuReceive(Long orderId, Long kefuId) {
        return baseMapper.findKefuReceive(orderId,kefuId);
    }

    @Override
    public BigDecimal sumKefuUpPayStatistics(AgentOrderQuery query, int profitSettleStateHasup) {
        // 已上交款
        BigDecimal hasUpPay = baseMapper.sumKefuUpPayStatistics(query.getAgentId(),GlobalConstant.PROFIT_SETTLE_STATE_HASUP,query.getStartTime(),query.getEndTime(),query.getSelectedAgentId());
        return hasUpPay;
    }
}
